import 'package:base_lib/common/base/base_theme_state.dart';
import 'package:base_lib/common/base/base_theme_widget.dart';
import 'package:base_lib/index.dart';
import 'package:base_lib/theme/theme_global.dart';
import 'package:doctor_prescribe_lib/doctor_prescribe_component.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'entity/diagnostic_result_list_entity.dart' as DiagnosticBean;

///诊断结果widget
class DiagnosticResultWidget extends StatefulWidget {
  final Function(List<DiagnosticBean.Data>) onChange;

  final TextStyle titleTextStyle;
  final TextStyle itemTextStyle;

  DiagnosticResultWidget(this.onChange,
      {this.titleTextStyle = const TextStyle(fontSize: 15, color: Colors.black),
      this.itemTextStyle = const TextStyle(fontSize: 13, color: Colors.black)});

  @override
  State<StatefulWidget> createState() {
    return _DiagnosticResultWidgetState();
  }
}

class _DiagnosticResultWidgetState
    extends BaseThemeState<DiagnosticResultWidget> {
  List<DiagnosticBean.Data> list = [];

  @override
  Widget buildChild(BuildContext context, MTheme theme) {
    return Container(
      child: Column(
        children: [
          Row(
            children: [
              Text(
                '诊断结果',
                style: widget.titleTextStyle,
              ),
              Spacer(),
              list.isEmpty
                  ? IconButton(
                      onPressed: () {
                        go2add();
                      },
                      icon: Icon(
                        Icons.add_circle_outline,
                        color: theme.themeColor.themeColor,
                      ),
                    )
                  : SizedBox(),
            ],
          ),
          list.isNotEmpty
              ? Column(
                  children: list.map((e) => _buildItem(e)).toList(),
                )
              : SizedBox(),
        ],
      ),
    );
  }

  Widget _buildItem(DiagnosticBean.Data e) {
    return DiagnosticResultItemWidget<DiagnosticBean.Data>(
      e.hisParaValue,
      delete: (item) {
        delete(item);
      },
      add: (item) {
        go2add();
      },
      data: e,
      textStyle: widget.itemTextStyle,
    );
  }

  delete(DiagnosticBean.Data item) {
    list.remove(item);
    widget.onChange(list);
    setState(() {});
  }

  go2add() {
    if (list.length == 3) {
      ToastUtil.warning('最多添加3个诊断结果');
      return;
    }

    Routers.navigateTo(context, DoctorPrescribeComponent.diagnosticResultPage)
        .then((value) {
      if (value is DiagnosticBean.Data) {
        list.add(value);
        widget.onChange(list);
        setState(() {});
      }
    });
  }
}

class DiagnosticResultItemWidget<T> extends BaseThemeWidget {
  final String des;
  final TextStyle textStyle;
  final T data;
  final Function(T) delete;
  final Function(T) add;

  DiagnosticResultItemWidget(this.des,
      {this.delete,
      this.add,
      this.data,
      this.textStyle = const TextStyle(fontSize: 13, color: Colors.black)});

  @override
  Widget buildChild(BuildContext context, MTheme theme) {
    return Container(
      decoration: BoxDecoration(
          border: Border(bottom: BorderSide(color: Colors.grey[300]))),
      margin: EdgeInsets.symmetric(horizontal: 10),
      child: Row(
        children: [
          Expanded(
            child: Text(
              des,
              style: textStyle,
            ),
          ),
          IconButton(
            onPressed: () {
              delete(data);
            },
            icon: Icon(
              Icons.remove_circle_outline,
              color: Colors.red,
            ),
          ),
          IconButton(
            onPressed: () {
              add(data);
            },
            icon: Icon(
              Icons.add_circle_outline,
              color: theme.themeColor.themeColor,
            ),
          ),
        ],
      ),
    );
  }
}
